<?php
$page_security = 'SA_SALESPAYMNT';
$path_to_root = "..";
include_once($path_to_root . "/includes/ui/allocation_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
//include_once($path_to_root . "/sales/includes/ui/cust_alloc_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");

$js = "";
if (true) {
	$js .= get_js_open_window(900, 500);
}
if ($use_date_picker) {
	$js .= get_js_date_picker();
}
add_js_file('payalloc.js');

page(_($help_context = "Penghitungan Harga Pokok Produksi"), false, false, "", $js);
simple_page_mode();

if(isset($_GET['notapesan']))
{
	$_POST['notapesan'] = $_GET['notapesan'];
}

if(isset($_GET['NewTrans']))
	unset($_SESSION['kb_Items']);

if(isset($_SESSION['kb_Items']))
	$line_items = $_SESSION['kb_Items'];
else
	$line_items = array();

$Refs = new references();

if (isset($_GET['AddedID'])) {
	$payment_no = $_GET['AddedID'];

	display_notification_centered(_("Harga Pokok Produksi berhasil disimpan"));

	//submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');

	//display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));

	hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", "Lihat semua Harga Pokok Produksi ", "trans_no=$payment_no&trans_type=12");

	hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Hitung Harga Pokok Produksi yang lain."));

	display_footer_exit();
}



//----------------------------------------------------------------------------------------------



if (isset($_POST['Save'])) {
	save_hpp(get_post('pref'), get_post('subtotalbb'),get_post('subtotaltkl'), get_post('subtotalbop'), get_post('notapesan'));    
	meta_forward("view/cari_hpp.php?");
    
}

start_form();
hidden('notapesan',$_POST['notapesan']);

	start_outer_table(TABLESTYLE2, "width=75%", 5);
	table_section(1);

//	kasbon_list_row("Kas Bon Ref:", 'kasbon_id', null, false, true);
//	$kasbon_id = $_POST['kasbon_id'];
//	$row = get_kas_bon(1);
	$pRef = $Refs->get_next(HPP);
//    label_row("Date", $row['date']);
//	label_row(_("Total Amount:"), price_format($row['amount']));
	label_row(_("Reference:"), $pRef);
	hidden('pref',$pRef);
	label_row(_("No. Nota Pesan:"), get_post('notapesan'));
//	table_section(2);
//	textarea_row(_("Keperluan:"), 'note', $row['note'], 50,5);

		end_outer_table(1);

	start_table(TABLESTYLE, "colspan=7 width=75%");
	//start_outer_table(TABLESTYLE2, "width=90%", 5);
	$th = array(_("Nama Kategori"), _("Nama"), _("Total Item"), _("Biaya Per Satuan"),_("Sub Total"),'');
	table_header($th);

	$total = 0;
	$k = 0;  //row colour counter

	$id = find_submit('Delete');
	$has_marked = false;

	$result = get_bahan_baku_production_card(get_post('notapesan'));

	$grant_total = $sub_total = 0;

	while($row = db_fetch($result))
	{
//		var_dump($row);
		start_row();
		label_cell('Bahan Baku');
		label_cell($row['description']);
		qty_cell($row['item_amount']);
		label_cell(price_format($row['price']),'align=right');
		//amount_cell(null, 'hrg');
		$sub_total = $row['item_amount'] * $row['price'];
		$grant_total += $sub_total;
		label_cell(price_format($sub_total),'align=right');
		label_cell('');
		end_row();
	}
	hidden('subtotalbb',$grant_total);
	$result = get_tenaga_kerja_production_card(get_post('notapesan'));

	$row = db_fetch($result);

//		var_dump($row);
		start_row();
		label_cell('Tenaga Kerja Langsung');
		label_cell('Jahit');
		qty_cell($row['jmlh']);
		//label_cell($row['jmlh']);
		amount_cells(null, 'amount_tkl');
		//amount_cell(null, 'hrg');


		$sub_total = $row['jmlh'] * input_num('amount_tkl');
		$grant_total += $sub_total;
		label_cell(price_format($sub_total),'align=right');
		hidden('subtotaltkl',$sub_total);
		label_cell('');
		end_row();

		start_row();
		label_cell('BOP');
		label_cell('Beban Overhead Pabrik');
		qty_cell('1');
		amount_cells(null, 'amount_bop');
		$sub_total = input_num('amount_bop');

		$grant_total += $sub_total;
		//amount_cell(null, 'hrg');
		label_cell(price_format($sub_total),'align=right');
		hidden('subtotalbop',$sub_total);
		label_cell('');
		end_row();

//	foreach ($line_items as $line_no=>$stock_item)
//	{
//		$total += $stock_item['amount'];
//		label_cell($stock_item['no_note']);
//		label_cell($stock_item['acc_id']);
//		label_cell($stock_item['note']);
//		label_cell(price_format($stock_item['amount']),'align=right');
//		//label_cell($stock_item['']);
//		delete_button_cell("Delete".$stock_item['id'], _("Delete"),
//				_('Remove line from document'));
//		end_row();
//	}

	alt_table_row_color($k);
//	start_row();
//
//	if(!isset($_POST['no_note']))
//		$_POST['no_note'] = '';
//
//	text_cells(null, 'no_note',$_POST['no_note'],20);
//	gl_all_accounts_list_cells(null, 'acc_id');
//	text_cells(null, 'note','',20,40);
//
//
//	amount_cells(null, 'amount');
//
//
//
//	$line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
//
//	//amount_cell($line_total, false, '','line_total');
//
//
//		submit_cells('AddItem', _("Add Item"), "colspan=2 align='center'",
//		    _('Add new item to document'), false);
//
//	end_row();

	$colspan = 4;

	$display_total = price_format($grant_total);

	start_row();
	label_cells("Jumlah Total", $display_total, "colspan=$colspan align=right","align=right");
	submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), false);
	end_row();

	end_table();
	br();

	submit_center('Save', "Simpan HPP", true, '', false);
	br();

end_form();
end_page();

?>